<%= stylesheet_link_tag("will_filter/filter") -%>

<% if results.size > 0 %>
<table class='wf_paginator'>
    <tr>
        <td style='text-align:left;vertical-align:middle;'>
            <%= paginate(results) %>
        </td>
        <td style='text-align:right;vertical-align:middle;'>
						Showing <%= results.offset_value + 1 %> - <%= results.offset_value + results.length %> of <%= results.total_count %>
        </td>
    </tr>
</table>

<table class='wf_results_table'>
  <thead>
    <tr>
      <%
        carray = [] # stores the order of the columns

        opts[:columns].each_with_index do |column, index|
          cmeta = {}

          # supported notations for columns => [
          #  :id,
          #  [:id, lambda{|obj| obj.id}],
          #  [:id, lambda{|obj| obj.id}, "color: white"],
          #  [:id, lambda{|obj| obj.id}, {:style => "color: white"}],
          #  [:id, {:filterable => true, :value => lambda{|obj| obj.id}}],
          #  [:id, {:filterable => true, :value => lambda{|obj| obj.id}}],
          #  {:key => :id, :filterable => true}
          # ]

          if column.is_a?(Array)
            if column.size < 2
              raise WillFilter::FilterException.new("Array table column definition must have at least two elements")
            end

            cmeta[:key] = column[0]  # first param must always be the column key

            if column[1].is_a?(Hash)  # second param is a hash
              cmeta.merge!(column[1])
            else
              cmeta[:value] = column[1]
              unless column[2].nil?
                if column[2].is_a?(Hash)   # third param can be a set of attributes
                  cmeta.merge!(column[2])
                else  # third param can be a string, which represents a style
                  cmeta[:style] = column[2]
                end
              end
            end
          elsif column.is_a?(Hash)
            cmeta = column
            cmeta[:key] ||= "key_#{index}"
          else
            cmeta[:key] = column
          end

          carray << cmeta

          sort_direction        = filter && filter.column_sorted?(cmeta[:key]) ? filter.order_type : "asc"
          sort_direction_click  = filter && filter.column_sorted?(cmeta[:key]) ? (sort_direction == "asc" ? "desc" : "asc") : "asc"

          if cmeta[:sort_key]
            sort_key = cmeta[:sort_key].call(filter)
          else
            sort_key = cmeta[:key]
          end

          if cmeta[:sortable].nil?
            sortable = filter && filter.contains_column?(cmeta[:key])
          else
            sortable = cmeta[:sortable]
          end

          if cmeta[:title].is_a?(Proc)
            column_title = cmeta[:title].call(filter)
          else
            # column_title = cmeta[:title] || filter ? filter.condition_title_for(cmeta[:key]) : "Undefined"
            if filter
              column_title = cmeta[:title] || filter.condition_title_for(cmeta[:key])
            else
              column_title = cmeta[:title] || cmeta[:key].to_s.titleize
            end

            if sortable
              column_title = link_to(column_title, filter.to_params(:wf_order => cmeta[:key], :wf_order_type => sort_direction_click), :title => "sort by #{cmeta[:key]} ascending", :class => "sort_link")
            end
          end

          # column class name
          title_class_name      = "wf_#{cmeta[:title_class] || cmeta[:key].to_s}"
          title_class_name      << " sortable"                 if sortable
          title_class_name      << " current_sort"             if filter && filter.column_sorted?(sort_key)
          title_class_name      << " #{sort_direction}"

          # extra styling that can be applied on a column
          title_style           = cmeta[:title_style] || ""
          title_style           << ";width:15px" if cmeta[:key] == :checkbox
        %>

        <th class="<%= title_class_name %>" style="<%=title_style%>">
          <div class="table_head_container">
            <% if sortable %>
              <div class="sort_control">
                <%= link_to "", filter.to_params(:wf_order => sort_key, :wf_order_type => 'asc'), :title => "sort by #{sort_key} ascending", :class => 'up' %>
                <%= link_to "", filter.to_params(:wf_order => sort_key, :wf_order_type => 'desc'), :title => "sort by #{sort_key} descending", :class => 'down' %>
              </div>

              <div style='white-space: nowrap;'>
                <%= column_title %>
              </div>

            <% elsif cmeta[:key] == :checkbox %>
              <%= check_box_tag("wf_check_all", "", false, :onClick => "wfCheckAll(this)") %>

            <% else %>
              <%= column_title %>

            <% end %>
          </div>
        </th>
      <% end %>
    </tr>
  </thead>

  <tbody>

    <% if opts[:header] %>
      <%=render(:partial => "/will_filter/common/results_table_summary", :locals => {:data => opts[:header], :filter => filter, :carray => carray}) %>
    <% end -%>

    <% results.each_with_index do |obj, index| %>
      <tr class='data_row <%=(index % 2 == 0) ? "wf_row_even" : "wf_row_odd"%>'>
        <% carray.each_with_index do |cmeta, column_index| %>
          <%
           if cmeta[:style].nil?
		value_style = 'padding-top:2px;padding-bottom:2px;vertical-align:middle;'
           elsif cmeta[:style].is_a?(Proc)
              value_style = cmeta[:style].call(obj)
            else
              value_style = cmeta[:style]
            end

            value_class = cmeta[:class] || ''

            if cmeta[:value].nil?
              value = obj.send(cmeta[:key])
            elsif cmeta[:value].is_a?(Proc)
              value = cmeta[:value].call(obj)
            else
              value = cmeta[:value]
            end

            value_style << 'width:30px;' if cmeta[:key] == :checkbox
            value_class << ' current_sort' if filter and filter.column_sorted?(cmeta[:key])

            if cmeta[:filterable]
              filter_value = cmeta[:value].nil? ? value : obj.send(cmeta[:key])
            end
          %>
          <td style='<%=value_style%>' class='<%=value_class%>'>
              <% if cmeta[:filterable] %>
                <div style='float:right;'><%=link_to(image_tag("will_filter/filter.png", :style=>"width:12px;"), results.add_filter_condition(cmeta[:key], :is, filter_value.to_s).to_params, {:title => "Show only #{filter_value}"}) %></div>
              <% end -%>

               <% if cmeta[:key] == :checkbox %>
                  <%= check_box_tag("#{value}[]", obj.id, false, :id => "wf_check_#{index}", :onClick => "wfVerifyTrigger(this)") %>

               <% elsif value.is_a?(Hash) %>
                  <% value.each do |key, value| %>
                    <strong><%=key%>:</strong> <%= value %>;
                  <% end -%>

                <% elsif value.is_a?(Time) %>
                  <%= value.strftime("%m/%d/%Y %l:%M:%S %p %Z") %>

                <% elsif value.is_a?(Date) %>
                  <%= value.strftime("%m/%d/%Y") %>

               <% else %>

                  <%= value.to_s %>
               <% end -%>
          </td>
        <% end -%>
      </tr>
    <% end -%>

    <% if opts[:footer] %>
      <%=render(:partial => "/will_filter/common/results_table_summary", :locals => {:data => opts[:footer], :filter => filter, :carray => carray}) %>
    <% end -%>

  </tbody>
</table>

<table class='wf_paginator'>
    <tr>
        <td style='text-align:left;vertical-align:middle;'>
            <%= paginate(results) %>
        </td>
        <td style='text-align:right;vertical-align:middle;'>
            Showing <%= results.offset_value + 1 %> - <%= results.offset_value + results.length %> of <%= results.total_count %>
        </td>
    </tr>
</table>
  
<script>
  function wfVerifyTrigger(check) {
    if (!check.checked) {
      var check_all = document.getElementById("wf_check_all");
      check_all.checked = false;
    }
  }

  function wfCheckAll(trigger) {
    var index = 0;
    var check = document.getElementById("wf_check_" + index);
    while (check) {
      check.checked = trigger.checked;
      check = document.getElementById("wf_check_" + (++index));
    }
  }
</script>

<% else %>

<table class='wf_paginator' style="margin-top:10px; margin-bottom:10px;">
    <tr>
        <td style='text-align:left'>
            No results found
        </td>
        <td style='text-align:right'>
        </td>
    </tr>
</table>

<% end %>
